//
//  SISSReserveCommissionedNotaryShowViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/24.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSReserveCommissionedNotaryShowViewController.h"
#import "SISSTitleView.h"
#import "SISSComplainCollectionViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SISSUploadPictureViewController.h"
#import "SISSHouseProgressViewController.h"
#import "SISSReserveCommissionedNotaryViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"

@interface SISSReserveCommissionedNotaryShowViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

/// 预约信息
@property (weak, nonatomic) SISSTitleView *reserveTitleView;
@property (weak, nonatomic) UILabel *nameTitleLabel;
@property (weak, nonatomic) UILabel *nameLabel;
@property (weak, nonatomic) UILabel *reserveTimeTitleLabel;
@property (weak, nonatomic) UILabel *timeLabel;
@property (weak, nonatomic) UILabel *addressTitleLabel;
@property (weak, nonatomic) UILabel *addressLabel;
/// 业主所需资料
@property (weak, nonatomic) SISSTitleView *requiredInfoTitleView;
@property (weak, nonatomic) UILabel *requiredInfoLabel;
/// 注意事项
@property (weak, nonatomic) SISSTitleView *noticeTitleView;
@property (weak, nonatomic) UILabel *noticeLabel;

/// 公证书附件
@property (weak, nonatomic) SISSTitleView *attachmentTitleView;
@property (weak, nonatomic) UICollectionView *attachmentCollectionView;

/// 编辑 上传
@property (weak, nonatomic) UIButton *editButton;
@property (weak, nonatomic) UIButton *uploadButton;

/// 照片数组
@property (copy ,nonatomic) NSArray *images;
/// 图片url数组
@property (copy ,nonatomic) NSArray *imagesUrlArr;

///
@property (assign, nonatomic) BOOL isConfirm;
@property (strong, nonatomic) SISSReserveCommissionedNotaryModel *notaryModel;

@end

@implementation SISSReserveCommissionedNotaryShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"公证委托";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)];
    [self requestData];
}

/**
 返回按钮点击事件
 */
- (void)backItemAction {
    
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[SISSHouseProgressViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
    
}

- (void)requestData {
    [kNetworkManager requestNotaryInfoWithAhbpId:self.ahbpId success:^(SISSReserveCommissionedNotaryModel *notaryModel) {
        self.notaryModel = notaryModel;
        if (self.isHistory && !notaryModel) {
            self.containerView.hidden = YES;
            self.editButton.hidden = YES;
            self.uploadButton.hidden = YES;
        }
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)setNotaryModel:(SISSReserveCommissionedNotaryModel *)notaryModel {
    _notaryModel = notaryModel;
    self.nameLabel.text = notaryModel.name;
    self.timeLabel.text = notaryModel.applyTime;
    self.addressLabel.text = notaryModel.address;
    self.requiredInfoLabel.text = notaryModel.file;
    self.noticeLabel.text = notaryModel.attention;

    self.imagesUrlArr = notaryModel.image;

    CGFloat itemW = (SCREEN_WIDTH - 3 * 1) / 3;
    CGFloat collectViewHeight = self.imagesUrlArr.count > 6 ? itemW * 3 : (self.imagesUrlArr.count > 3 ? itemW * 2 : itemW);
    [self.attachmentCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(collectViewHeight);
    }];
    [self.attachmentCollectionView reloadData];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return  self.images.count > 0 ? self.images.count : self.imagesUrlArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSComplainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
    
    if (self.images.count > 0) {
        cell.imageView.image = self.images[indexPath.row];
    } else {
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imagesUrlArr[indexPath.item]]];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
}


// 编辑按钮点击事件
- (void)editButtonClickAction {
    SISSReserveCommissionedNotaryViewController *vc = [[SISSReserveCommissionedNotaryViewController alloc] init];
    vc.ahbpId = self.ahbpId;
    vc.notaryModel = self.notaryModel;
    [self.navigationController pushViewController:vc animated:YES];
}

// 上传公证书按钮点击事件
- (void)uploadButtonClickAction {
    
    SISSUploadPictureViewController *vc = [[SISSUploadPictureViewController alloc] init];
    vc.title = @"公证委托信息";
    vc.titleStr = @"上传公证书";
    vc.ID = self.notaryModel.ID;
    vc.selectedImagesBlock = ^(NSArray * _Nonnull images) {
        self.images = images;
        self.isConfirm = YES;
        [self requestData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)setupSubviews {
    [super setupSubviews];
    
    SISSTitleView *reserveTitleView = [[SISSTitleView alloc] init];
    reserveTitleView.titleStr = @"预约公证";
    [self.containerView addSubview:reserveTitleView];
    self.reserveTitleView = reserveTitleView;
    
    UILabel *nameTitleLabel = [UILabel yyr_labelWithText:@"公证处办事处：" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:nameTitleLabel];
    self.nameTitleLabel = nameTitleLabel;
    
    UILabel *nameLabel = [UILabel yyr_labelWithText:@"" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:nameLabel];
    self.nameLabel = nameLabel;
   
    
    UILabel *reserveTimeTitleLabel = [UILabel yyr_labelWithText:@"预约时间段：" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:reserveTimeTitleLabel];
    self.reserveTimeTitleLabel = reserveTimeTitleLabel;
    
    UILabel *timeLabel = [UILabel yyr_labelWithText:@"" fontSize:15 textColor:MAIN_TEXT_COLOR];
    
    [self.containerView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *addressTitleLabel = [UILabel yyr_labelWithText:@"公证处办事处地址：" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:addressTitleLabel];
    self.addressTitleLabel = addressTitleLabel;
    
    UILabel *addressLabel = [UILabel yyr_labelWithText:@"{地址}" fontSize:15 textColor:MAIN_TEXT_COLOR ];
    [self.containerView addSubview:addressLabel];
    self.addressLabel = addressLabel;
    
    SISSTitleView *requiredInfoTitleView = [[SISSTitleView alloc] init];
    requiredInfoTitleView.titleStr = @"业主所需资料";
    [self.containerView addSubview:requiredInfoTitleView];
    self.requiredInfoTitleView = requiredInfoTitleView;
    
    UILabel *requiredInfoLabel = [UILabel yyr_labelWithText:@"" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:requiredInfoLabel];
    self.requiredInfoLabel = requiredInfoLabel;
    
    SISSTitleView *noticeTitleView = [[SISSTitleView alloc] init];
    noticeTitleView.titleStr = @"注意事项";
    [self.containerView addSubview:noticeTitleView];
    self.noticeTitleView = noticeTitleView;
    
    UILabel *noticeLabel = [UILabel yyr_labelWithText:@"" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:noticeLabel];
    self.noticeLabel = noticeLabel;
    
    SISSTitleView *attachmentTitleView = [[SISSTitleView alloc] init];
    attachmentTitleView.titleStr = @"公证书附件";
    [self.containerView addSubview:attachmentTitleView];
    self.attachmentTitleView = attachmentTitleView;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    CGFloat itemW = (SCREEN_WIDTH - 3 * 10) / 3;
    flowLayout.itemSize = CGSizeMake(itemW, itemW);
    flowLayout.sectionInset = UIEdgeInsetsMake(1, 10, 1, 10);
    UICollectionView *attachmentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    attachmentCollectionView.backgroundColor = [UIColor whiteColor];
    attachmentCollectionView.dataSource = self;
    attachmentCollectionView.delegate = self;
    [attachmentCollectionView registerNib:[UINib nibWithNibName:@"SISSComplainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ID"];
    [self.containerView addSubview:attachmentCollectionView];
    self.attachmentCollectionView = attachmentCollectionView;
    
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editButton setTitle:@"编辑预约信息" forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editButton setBackgroundColor:MAIN_COLOR];
    [editButton addTarget:self action:@selector(editButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:editButton];
    self.editButton = editButton;
    
    UIButton *uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [uploadButton setTitle:@"上传公证证书" forState:UIControlStateNormal];
    [uploadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [uploadButton setBackgroundColor:MAIN_COLOR];
    [uploadButton addTarget:self action:@selector(uploadButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:uploadButton];
    self.uploadButton = uploadButton;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf;
    [self.reserveTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.containerView);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.nameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.reserveTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.containerView).offset(2 * globelMargin);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.nameTitleLabel);
        make.left.equalTo(weakSelf.nameTitleLabel.mas_right).offset(0.5 * globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin).priority(200);
        make.height.mas_equalTo(3 * globelMargin);
    }];
    
    [self.reserveTimeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.nameTitleLabel.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.nameTitleLabel);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.reserveTimeTitleLabel);
        make.left.equalTo(weakSelf.nameTitleLabel.mas_right).offset(0.5 * globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin).priority(200);
        make.height.mas_equalTo(3 * globelMargin);
    }];
    
    [self.addressTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.reserveTimeTitleLabel.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.reserveTimeTitleLabel);
    }];
    
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.addressTitleLabel);
        make.left.equalTo(weakSelf.addressTitleLabel.mas_right).offset(0.5 * globelMargin);
    }];
    
    [self.requiredInfoTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.addressTitleLabel.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.requiredInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.requiredInfoTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.containerView).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
        make.height.mas_equalTo(8 * globelMargin);
    }];
    
    [self.noticeTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.requiredInfoLabel.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.noticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.noticeTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.containerView).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
        make.height.mas_equalTo(8 * globelMargin);
    }];
    
    [self.attachmentTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.noticeLabel.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.attachmentCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.attachmentTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(8 * globelMargin);
        make.bottom.equalTo(weakSelf.containerView).offset(-7 * globelMargin);
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.mas_bottomLayoutGuideTop);
        make.right.equalTo(weakSelf.view.mas_centerX);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.uploadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.view);
        make.left.equalTo(weakSelf.view.mas_centerX);
        make.bottom.height.equalTo(weakSelf.editButton);
    }];
}



@end
